package com.fau.wm.action.index;

import java.io.File;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.fau.wm.action.PublicAction;
import com.fau.wm.entity.MedicalAdmin;
import com.fau.wm.entity.MedicalStorageDetail;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @desc 入库明细Action
 * @author kangyj
 * @datetime 2014年8月4日,下午4:20:49
 */
@Controller
@Scope("prototype")
@ParentPackage("default")
@Namespace("/")
public class StorageDetailAction extends PublicAction implements ModelDriven<MedicalStorageDetail> {

	private static final long serialVersionUID = -3014471481728644034L;
	
	private MedicalStorageDetail storageDetail = new MedicalStorageDetail();
	
	/**
	 * @desc 批量导入入库药品
	 * @return String
	 * @author liy
	 * @datetime 2014年12月23日,下午10:38:31
	 */
	@Action(value = "storage_import_view", results = {@Result(name = SUCCESS, location = "storage_import_view.jsp")})
	public String importView() {
		return SUCCESS;
	}
	
	/**
	 * @desc 入库单明细页面初始化
	 * @return
	 * @author kangyj
	 * @datetime 2014年8月2日,下午4:34:45
	 */
	@Action(value = "storage_detail_init", results = {@Result(name = SUCCESS, location = "storage_detail_init.jsp")})
	public String init(){
		return SUCCESS;
	}
	
	/**
	 * @desc 根据入库单ID查询入库明细
	 * @return null
	 * @author kangyj
	 * @datetime 2014年8月4日,下午4:29:07
	 */
	@Action(value = "storage_detail_view")
	public String view(){
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		String key = request.getParameter("key");
		list = this.baseAllService.getStorageManagerService().findStorageDetailList(key);
		formatJSON(response, list);
		return null;
	}
	
	/**
	 * @desc 查询包含特定药品的入库明细
	 * @return null
	 * @author liy
	 * @datetime 2014年12月20日,下午4:40:11
	 */
	@Action(value = "storage_medicine_detail")
	public String storageMedicineDetail(){
		HttpServletResponse response = ServletActionContext.getResponse();
		this.map = ActionContext.getContext().getParameters();
		if(StringUtils.isNotBlank(this.storageDetail.getMedicineId()) 
				&& StringUtils.isNotBlank(this.storageDetail.getWarehouseId())){
			this.pageBean = this.baseAllService.getStorageManagerService().findMedicineStorageDetailList(this.storageDetail.getMedicineId()
					,this.storageDetail.getWarehouseId(),this.map);
			
			this.map.clear();
			map.put("total", this.pageBean.getTotalRow());
			map.put("data", this.pageBean.getResult());
			formatJSON(response, this.map);
		}
		
		return null;
	}
	
	/**
	 * @desc 添加入库明细
	 * @return
	 * @author kangyj
	 * @datetime 2014年8月4日,下午4:29:59
	 */
	@Action(value = "storage_detail_add", results = {@Result(name = SUCCESS, location = "storage_detail_add.jsp")})
	public String add(){
		return SUCCESS;
	}
	
	/**
	 * @desc 按拼音模糊查询药品
	 * @return null
	 * @author kangyj
	 * @datetime 2014年8月4日,下午8:40:06
	 */
	@Action(value = "storage_detail_find_medicine")
	public String findMedicineByName(){
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		String key = request.getParameter("key");
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		list = this.baseAllService.getMedicineService().findMedicineByName(admin, key);
		formatJSON(response, list);
		return null;
	}
	
	/**
	 * @desc 保存入库明细
	 * @return
	 * @author kangyj
	 * @datetime 2014年8月5日,下午5:24:01
	 */
	@Action(value = "storage_detail_save")
	public String save(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		Boolean flag = this.baseAllService.getStorageManagerService().saveStorageDetail(storageDetail);
		if(flag){
			map.put("success", true);
		}else{
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 删除入库明细
	 * @return
	 * @author kangyj
	 * @datetime 2014年8月6日,上午10:04:14
	 */
	@Action(value = "storage_detail_del")
	public String del(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(storageDetail.getId())){
			this.baseAllService.getStorageManagerService().delStorageDetail(storageDetail);
			map.put("success", true);
		}else{
			map.put("message", "error");
			}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 编辑入库明细表
	 * @return
	 * @author kangyj
	 * @datetime 2014年8月6日,下午3:49:04
	 */
	@Action(value = "storage_detail_edit", results = {@Result(name = SUCCESS, location = "storage_detail_edit.jsp")})
	public String edit(){
		return SUCCESS;
	}
	
	/**
	 * @desc 查询入库详细表对象
	 * @return
	 * @author kangyj
	 * @datetime 2014年8月6日,下午4:51:27
	 */
	@Action(value = "storage_detail_find")
	public String findStorageDetail(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(storageDetail.getId())){
			storageDetail = this.baseAllService.getStorageManagerService().findStorageDetailById(storageDetail.getId());
			map.put("storageDetail", storageDetail);
		}else{
			map.put("message", "error");
			}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 编辑保存入库明细
	 * @return
	 * @author kangyj
	 * @datetime 2014年8月7日,下午2:55:39
	 */
	@Action(value = "storage_detail_edit_save")
	public String editSave(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(storageDetail.getId())){
			this.baseAllService.getStorageManagerService().editSaveStorageDetail(storageDetail);
			map.put("success", true);
		}else{
			map.put("message", "error");
			}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 检查入库明细表中是否已存在将要添加的药品
	 * @return
	 * @author kangyj
	 * @datetime 2014年8月8日,下午2:44:45
	 */
	@Action(value = "storage_detail_check_medicine")
	public String checkMedicineExist(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(storageDetail.getStorageId()) && StringUtils.isNotBlank(storageDetail.getMedicineId())){
			Boolean flag = this.baseAllService.getStorageManagerService().checkMedicineExist(storageDetail.getStorageId(), storageDetail.getMedicineId());
			if(flag){
				map.put("able", true);
			}else{
				map.put("able", false);
			}
		}else{
			map.put("message", "error");
			}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 批量导入入库药品
	 * @return null
	 * @author liy
	 * @datetime 2014年12月23日,下午11:02:18
	 */
	@Action(value="import_storage_medicine")
	public String importStorageMedicine(){
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		map = new HashMap<String, Object>();
		File file = this.getImportFile(request);
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		try {
			String info = this.baseAllService.getStorageManagerService().improtStorageMedicine(admin,storageDetail.getStorageId(), file);
			if(StringUtils.isBlank(info)){
				map.put("success", true);
				map.put("message", "导入成功！");
				formatJSON(response, map);
			}else{
				map.put("success", false);
				map.put("message", info);
				formatJSON(response, map);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public MedicalStorageDetail getStorageDetail() {
		return storageDetail;
	}

	public void setStorageDetail(MedicalStorageDetail storageDetail) {
		this.storageDetail = storageDetail;
	}

	@Override
	public MedicalStorageDetail getModel() {
		return storageDetail;
	}

}
